import os

import yaml

from util.loguru_util import myLogger
import traceback


def read_app_config():
    root_path = get_root_path()
    config_path = os.path.join(root_path, 'application.yaml')
    try:
        with open(config_path, "r", encoding="utf-8") as f:
            return yaml.load(f, Loader=yaml.FullLoader)
    except Exception as e:
        myLogger.error("get application.yaml failed ")
        myLogger.error(traceback.extract_stack())
        return None


def read_database_config():
    config = read_app_config()
    if config:
        return config['database']
    else:
        return None


def read_fastapi_config():
    config = read_app_config()
    if config:
        return config['fastapi']
    else:
        return None


def get_root_path():
    current_path = os.path.abspath(os.path.dirname(__file__))
    root_path = os.path.dirname(current_path)
    return root_path


def get_business_config():
    config = read_app_config()
    if config:
        return config['business']
    else:
        return None
